package com.st.event.demo.proxy.factory;

import com.st.event.demo.post.OrderFinishTriggerListener;
import com.st.event.demo.source.EventAttribute;
import com.st.event.demo.source.EventAttributeSource;
import com.st.event.demo.strategy.EventStrategy;
import org.springframework.lang.UsesJava7;

import java.io.Serializable;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;

public class EventClientProxy<T> implements InvocationHandler, Serializable {

    private final Class<T> clientInterface;

    private EventAttributeSource eventAttributeSource;

    private EventStrategy eventStrategy;

    private Map<String, OrderFinishTriggerListener> listenerMap;

    public EventClientProxy(Class<T> clientInterface,
                            EventAttributeSource eventAttributeSource,
                            EventStrategy eventStrategy,
                            Map<String, OrderFinishTriggerListener> listenerMap) {
        this.clientInterface = clientInterface;
        this.eventStrategy = eventStrategy;
        this.eventAttributeSource = eventAttributeSource;
        this.listenerMap = listenerMap;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, args);
        } else if (isDefaultMethod(method)) {
            return invokeDefaultMethod(proxy, method, args);
        }
        EventAttribute eventAttribute = parseAnnotationMetaInfo(method);
        return getEventStrategy().doExecute(eventAttribute, getClientInterface(), method, args, getListenerMap());
    }

    private EventAttribute parseAnnotationMetaInfo(Method method) {
        return getEventAttributeSource().getPayAttribute(method, getClientInterface());
    }

    @UsesJava7
    private Object invokeDefaultMethod(Object proxy, Method method, Object[] args)
            throws Throwable {
        final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class
                .getDeclaredConstructor(Class.class, int.class);
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        final Class<?> declaringClass = method.getDeclaringClass();
        return constructor
                .newInstance(declaringClass,
                        MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
                                | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC)
                .unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
    }

    /**
     * Backport of java.lang.reflect.Method#isDefault()
     */
    private boolean isDefaultMethod(Method method) {
        return (method.getModifiers()
                & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) == Modifier.PUBLIC
                && method.getDeclaringClass().isInterface();
    }

    public EventAttributeSource getEventAttributeSource() {
        return eventAttributeSource;
    }

    public EventStrategy getEventStrategy() {
        return eventStrategy;
    }

    public Map<String, OrderFinishTriggerListener> getListenerMap() {
        return listenerMap;
    }

    public Class<T> getClientInterface() {
        return clientInterface;
    }
}
